import React, { Component, useContext, useState, useEffect, useReducer } from 'react'
import UseReducerChild from './useReducerChild'
let a = 0
export default function UseReducer () {
  const initialState = { count: 0 }

  function reducer (state, action) {
    switch (action.type) {
      case 'increment':
        a += 1
        console.log(a)
        return { count: state.count + action.payload }
      case 'decrement':
        a += 1
        console.log(a)
        return { count: state.count + action.payload }
      default:
        throw new Error()
    }
  }
  const [state, dispatch] = useReducer(reducer, initialState.count, (init) => {
    return { count: init + a }
  })
  return (
    <div>
      {state.count}
      <UseReducerChild dispatch={dispatch}></UseReducerChild>
      {/* <button onClick={() => dispatch({ type: 'decrement', payload: 9 })}>+9</button>
      <button onClick={() => dispatch({ type: 'increment', payload: -6 })}>-6</button> */}
    </div>
  )
}
